import axios from 'axios'

const api=axios.create({//axios实例化
    baseURL:'http://49.235.98.65:3000',//baseURL做url前缀
    timeout:200000,
    // url:'user/list'//设后缀
});
api.interceptors.request.use(config=>{//请求拦截器/////当localStorage无数据
    var user=JSON.parse(localStorage.getItem('temp')||'{}')._id||'';
    // console.log(user);
    //config.headers[user]//60a8a71256ae60584bb0b37f///60a89d3756ae60584bb0b37b
    config.headers.user=user////////

    console.log('config',config);
    return config;
},function(error){
    return Promise.reject(error);
});

let get = async function (url,data){//封装
    const response = await api.get(url,{
        params:data
    }).catch(err=>{
        errhandler(err);
    })
    return response.data;
}

let post = async function (url,data){//封装
    const response = await api.post(url,data)
    .catch(err=>{
        errhandler(err)
    })
    return response.data
}

let put = async function (url,data){//封装
    const response = await api.put(url,data)
    .catch(err=>{
        errhandler(err)
    })
    return response.data
} 

let del=async function (url,data) {
    const response = await api.request({data,url,method:'delete'})
    .catch(err=>{
        errhandler(err)
    })
    return response.data    
}
//错误处理公共方法
function errhandler(err) {
    console.log('err==>',err);
    console.log('err==>',err.response.data.message);
    alert(err.response.data.message)
}

export {//导出，页面中引用
    get,post,put,del
}